package android.apps.adapter;

import android.apps.R;
import android.apps.bean.ComparatorAddr;
import android.apps.bean.ComparatorTime;
import android.apps.bean.PhotoUpImageItem;
import android.content.Context;
import android.graphics.Bitmap.Config;
import android.location.Address;
import android.location.Geocoder;
import android.media.ExifInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

public class AlbumItemAdapter extends BaseAdapter {
    private List<PhotoUpImageItem> mList;
    private LayoutInflater layoutInflater;
    private ImageLoader imageLoader;
    private DisplayImageOptions options;
    private Context mContext;

    public AlbumItemAdapter(List<PhotoUpImageItem> list, Context context) {
        mContext=context;
        this.mList = list;
        layoutInflater = LayoutInflater.from(context);
        imageLoader = ImageLoader.getInstance();
        // 使用DisplayImageOption.Builder()创建DisplayImageOptions
        options = new DisplayImageOptions.Builder()
                .showStubImage(R.drawable.album_default_loading_pic) // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.album_default_loading_pic) // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.album_default_loading_pic) // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true) // 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中
                        // .displayer(new RoundedBitmapDisplayer(20)) // 设置成圆角图片
                .bitmapConfig(Config.ARGB_8888)
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
                .build(); // 创建配置过的DisplayImageOption对象
    }


    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.album_item_images_item_view, parent, false);
            holder = new Holder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.image_item);
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.check);
            holder.addname = (TextView) convertView.findViewById(R.id.addname);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        //图片加载器的使用代码，就这一句代码即可实现图片的加载。请注意
        //这里的uri地址，因为我们现在实现的是获取本地图片，所以使
        //用"file://"+图片的存储地址。如果要获取网络图片，
        //这里的uri就是图片的网络地址。
        imageLoader.displayImage("file://" + mList.get(position).getImagePath(), holder.imageView, options);
        Log.i("LCY", "uri==" + "file://" + mList.get(position).getImagePath());
        holder.checkBox.setChecked(mList.get(position).isSelected());
        holder.addname.setText(mList.get(position).getAddress());
        return convertView;
    }

    class Holder {
        TextView addname;
        ImageView imageView;
        CheckBox checkBox;
    }
}
